home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1377 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: ub239.dialup.uwa.edu.au!not-for-mail
  2. From: prye@cyllene.uwa.edu.au (Peter Rye)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is wrong in this code?
  5. Date: 13 Jan 1996 14:54:55 +0800
  6. Organization: The University of Western Australia
  7. Message-ID: <4d7kvv$j8@ub239.dialup.uwa.edu.au>
  8. References: <DL2z7o.2K5@scisun.sci.ccny.cuny.edu>
  9. NNTP-Posting-Host: ub239.dialup.uwa.edu.au
  10.  
  11. sergio@sci.ccny.cuny.edu (Sergio Rojas) writes:
  12.  
  13. Hi Sergio,
  14.  
  15. You have demonstrated a couple of common problems here.
  16. Possibly more...I'm sure we'll both find out soon enough.
  17.  
  18.  
  19. >#include <stdio.h>
  20. >#include <math.h>
  21. >main()
  22. >{
  23. >double x;
  24.  
  25. >printf("\n Enter a number \n");
  26.  
  27. >scanf("%f", &x);
  28.         ^-------------- You have told the compiler a lie.
  29.                         &x is the address of a double and
  30.                         you've told it to expect a float.
  31.                         Should be "%lf"...
  32.  
  33. >printf("\n %e to the power %e is equal to   ", x , x );
  34.  
  35. >x = pow(x,x);
  36.  ^-------------------- This line is a problem.
  37.                        I believe this will give rise to 
  38.                        "undefined behavior" because you 
  39.                        are modifying x using a function
  40.                        which takes x as an argument.
  41.                        Use something like y = pow(x,x);
  42.                        Where y is declared as a double,
  43.                        then use y in the printf below. 
  44.  
  45. >printf(" %12.6f \n", x );
  46.  
  47. >}
  48.  
  49. >Some output are as follows:
  50.  
  51. > Enter a number 
  52. >2
  53.  
  54. > 2.000000e+00 to the power 2.000000e+00 is equal to        4.000000 
  55. >scisun{sergio}121% a.out
  56.  
  57. > Enter a number 
  58. >3
  59.  
  60. > 3.200000e+01 to the power 3.200000e+01 is equal to    1461501637330902918203684832716283019655932542976.000000 
  61.  
  62.  
  63. >  Thanks for your comments,
  64.  
  65. >rojas
  66. >Email: sergio@scisun.sci.ccny.cuny.edu
  67.  
  68. -- 
  69. | Peter Rye                                                           |
  70. | Respiratory Research Fellow                                         |
  71. | Princess Margaret Hospital for Children                             |
  72. | Perth, Western Australia                                            |
  73.